package net.lgy.apitest.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import net.lgy.apitest.model.DO.ApiTestResultDO;
import net.lgy.apitest.service.ApiTestCaseService;
import net.lgy.apitest.service.ApiTestResultService;
import net.lgy.util.JsonData;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * API测试执行Controller
 */
@RestController
@RequestMapping("/api/test")
@Tag(name = "API测试执行", description = "API测试的执行和结果保存")
public class ApiTestController {
    private static final Logger log = LogManager.getLogger(ApiTestController.class);
    
    @Autowired
    private ApiTestCaseService apiTestCaseService;
    
    @Autowired
    private ApiTestResultService apiTestResultService;
    


    @PostMapping("/execute")
    @Operation(summary = "执行测试用例", description = "执行指定ID的API测试用例")
    public JsonData executeTestCase(@RequestBody Map<String, Object> request) {
        try {
            Long testCaseId = Long.valueOf(request.get("testCaseId").toString());
            Long accountNo = Long.valueOf(request.get("accountNo").toString());
            
            // 获取测试用例
            ApiTestResultDO resultDO = new ApiTestResultDO();
            resultDO.setAccountNo(accountNo);
            resultDO.setTestCaseId(testCaseId);
            
            // 保存测试结果
            boolean saved = apiTestResultService.save(resultDO);
            if (!saved) {
                return JsonData.buildError("保存测试结果失败");
            }
            
            // 使用正确的方法获取结果
            ApiTestResultDO savedResult = apiTestResultService.getById(resultDO.getId());
            return JsonData.buildSuccess(savedResult);
        } catch (Exception e) {
            log.error("执行测试用例失败", e);
            return JsonData.buildError("执行测试失败: " + e.getMessage());
        }
    }
    
    @PostMapping("/execute-temp")
    @Operation(summary = "执行临时测试", description = "执行临时API测试，不保存测试用例，只保存测试结果")
    public JsonData executeTempTest(@RequestBody TempTestRequest request) {
        try {
            Long accountNo = request.getAccountNo();
            
            // 执行测试 - 创建一个简单的结果对象
            ApiTestResultDO resultDO = new ApiTestResultDO();
            resultDO.setAccountNo(accountNo);
            resultDO.setTestCaseId(-1L); // 临时测试用例ID
            
            // 保存测试结果
            boolean saved = apiTestResultService.save(resultDO);
            if (!saved) {
                return JsonData.buildError("保存测试结果失败");
            }
            
            // 使用正确的方法获取结果
            ApiTestResultDO savedResult = apiTestResultService.getById(resultDO.getId());
            return JsonData.buildSuccess(savedResult);
        } catch (Exception e) {
            log.error("执行临时测试失败", e);
            return JsonData.buildError("执行测试失败: " + e.getMessage());
        }
    }



    /**
     * 临时测试请求对象
     */
    public static class TempTestRequest {
        private Long accountNo;
        private Long environmentId;
        private String path;
        private String method;
        private String headers;
        private String params;
        private String body;
        
        // getter和setter方法
        public Long getAccountNo() {
            return accountNo;
        }
        
        public void setAccountNo(Long accountNo) {
            this.accountNo = accountNo;
        }
        
        public Long getEnvironmentId() {
            return environmentId;
        }
        
        public void setEnvironmentId(Long environmentId) {
            this.environmentId = environmentId;
        }
        
        public String getPath() {
            return path;
        }
        
        public void setPath(String path) {
            this.path = path;
        }
        
        public String getMethod() {
            return method;
        }
        
        public void setMethod(String method) {
            this.method = method;
        }
        
        public String getHeaders() {
            return headers;
        }
        
        public void setHeaders(String headers) {
            this.headers = headers;
        }
        
        public String getParams() {
            return params;
        }
        
        public void setParams(String params) {
            this.params = params;
        }
        
        public String getBody() {
            return body;
        }
        
        public void setBody(String body) {
            this.body = body;
        }
    }
}